package cn.edu.neusoft.wuguangsheng.flowerimpression.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.adapter.MyFragmentPagerAdapter;

/**
 * A simple {@link Fragment} subclass.
 */
public class SheQFragment extends BaseFragment implements View.OnClickListener,ViewPager.OnPageChangeListener {

    private ViewPager myviewpager;
    private ArrayList<Fragment> fragments;

    //选项卡中的按钮
    private Button btn_first;
    private Button btn_second;
    private Button btn_third;
    //作为指示标签的按钮
    private ImageView cursor;
    //标志指示标签的横坐标
    float cursorX = 0;
    //所有按钮的宽度的数组
    private int[] widthArgs;
    //所有标题按钮的数组
    private Button[] btnArgs;

    @Override
    void initView() {
        initViews();




    }

    @Override
    void initEvent() {

        btn_first.setOnClickListener(this);
        btn_second.setOnClickListener(this);
        btn_third.setOnClickListener(this);



    }

    @Override
    void initDate() {

    }

    //初始化布局
    public void initViews(){
        myviewpager = (ViewPager)this.view.findViewById(R.id.myviewpager);
        btn_first = (Button)view.findViewById(R.id.btn_first);
        btn_second = (Button)view.findViewById(R.id.btn_second);
        btn_third = (Button)view.findViewById(R.id.btn_third);

        //初始化按钮数组
        btnArgs = new Button[]{btn_first,btn_second,btn_third};
        //指示标签设置为红色
        cursor = (ImageView)view.findViewById(R.id.cursor_btn);
        cursor.setBackgroundColor(Color.RED);

        btn_first.setOnClickListener(this);
        btn_second.setOnClickListener(this);
        btn_third.setOnClickListener(this);




        myviewpager.setOnPageChangeListener(this);

        fragments = new ArrayList<Fragment>();
        fragments.add(new MeiTuFragment());
        fragments.add(new SheQuFragment());
        fragments.add(new WenDaFragment());


        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getActivity().getSupportFragmentManager(),fragments);
        myviewpager.setAdapter(adapter);

        //先重置所有按钮颜色
        resetButtonColor();
        //再将第一个按钮字体设置为红色，表示默认选中第一个
        btn_first.setTextColor(Color.RED);


        btnArgs = new Button[]{btn_first,btn_second,btn_third};
        btn_first.post(new Runnable(){
            @Override
            public void run() {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)cursor.getLayoutParams();
                //减去边距*2，以对齐标题栏文字
                lp.width = btn_first.getWidth()-btn_first.getPaddingLeft()*2;
                cursor.setLayoutParams(lp);
                cursor.setX(btn_first.getPaddingLeft());
            }
        });

    }

    //重置所有按钮的颜色
        public void resetButtonColor(){
        btn_first.setBackgroundColor(Color.parseColor("#ffffff"));
        btn_second.setBackgroundColor(Color.parseColor("#ffffff"));
        btn_third.setBackgroundColor(Color.parseColor("#ffffff"));

        btn_first.setTextColor(Color.BLACK);
        btn_second.setTextColor(Color.BLACK);
        btn_third.setTextColor(Color.BLACK);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_first:
                myviewpager.setCurrentItem(0);
                cursorAnim(0);
                break;
            case R.id.btn_second:
                myviewpager.setCurrentItem(1);
                cursorAnim(1);
                break;
            case R.id.btn_third:
                myviewpager.setCurrentItem(2);
                cursorAnim(2);
                break;



    }}

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int arg0) {

        if (widthArgs == null){
            widthArgs = new int[]{btn_first.getWidth(),
            btn_second.getWidth(),
            btn_third.getWidth()};
        }
        // TODO Auto-generated method stub
        //每次滑动首先重置所有按钮的颜色
        resetButtonColor();
        //将滑动到的当前按钮颜色设置为红色
        btnArgs[arg0].setTextColor(Color.RED);
        cursorAnim(arg0);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        setLayout_file(R.layout.fragment_she_q);
        return super.onCreateView(inflater,container,savedInstanceState);
    }


    //指示器的跳转，传入当前所处的页面的下标
    public void cursorAnim(int curItem){
        //每次调用，就将指示器的横坐标设置为0，即开始的位置
        cursorX = 0;
        //再根据当前的curItem来设置指示器的宽度
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)cursor.getLayoutParams();
        //减去边距*2，以对齐标题栏文字
        lp.width = widthArgs[curItem]-btnArgs[0].getPaddingLeft()*2;
        cursor.setLayoutParams(lp);
        //循环获取当前页之前的所有页面的宽度
        for(int i=0; i<curItem; i++){
            cursorX = cursorX + btnArgs[i].getWidth();
        }
        //再加上当前页面的左边距，即为指示器当前应处的位置
        cursor.setX(cursorX+btnArgs[curItem].getPaddingLeft());
    }

}









